package com.example.myapplication1.behavior;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewParent;

import androidx.annotation.ColorInt;
import androidx.annotation.FloatRange;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.core.view.NestedScrollingParent;
import androidx.core.view.NestedScrollingParent2;
import androidx.core.view.NestedScrollingParent3;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.RecyclerView;

/**
 * Copyright (C), 2020, 重庆八进制有限公司
 *
 * @author: weng
 * @last-modifier: weng
 * @version: 1.0
 * @create-date: 2020/11/19 17:04
 * @last-modify-date: 2020/11/19 17:04
 * @description:
 */
public class MyBehavior<V> extends CoordinatorLayout.Behavior {

    float deltaY;

    public MyBehavior() {
    }

    public MyBehavior(Context context, AttributeSet attrs) {
        super(context, attrs);

    }

    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
        Log.e("tag","layoutDependsOn");
        return dependency instanceof RecyclerView;
    }

    @Override
    public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
        if (deltaY == 0) {
            deltaY = dependency.getY() - child.getHeight();
        }

        float dy = dependency.getY() - child.getHeight();
        dy = dy < 0 ? 0 : dy;
        float y = -(dy / deltaY) * child.getHeight();
        child.setTranslationY(y);
        Log.e("tag","onDependentViewChanged");
        return true;
    }

    @Override
    public void onAttachedToLayoutParams(@NonNull CoordinatorLayout.LayoutParams params) {
        Log.e("tag","onAttachedToLayoutParams");

    }

    @Override
    public void onDetachedFromLayoutParams() {
        Log.e("tag","onDetachedFromLayoutParams");
    }

    @Override
    public boolean onInterceptTouchEvent(@NonNull CoordinatorLayout parent, @NonNull View child,
                                         @NonNull MotionEvent ev) {
        Log.e("tag","onInterceptTouchEvent");
        return super.onInterceptTouchEvent(parent, child, ev);
    }

    @Override
    public boolean onTouchEvent(@NonNull CoordinatorLayout parent, @NonNull View child,
                                @NonNull MotionEvent ev) {

        Log.e("tag","onTouchEvent");
        return super.onTouchEvent(parent, child, ev);
    }


    @ColorInt
    @Override
    public int getScrimColor(@NonNull CoordinatorLayout parent, @NonNull View child) {
        Log.e("tag","getScrimColor");
        return Color.BLACK;
    }

    @Override
    @FloatRange(from = 0, to = 1)
    public float getScrimOpacity(@NonNull CoordinatorLayout parent, @NonNull View child) {
        Log.e("tag","getScrimOpacity");
        return 0.f;
    }


    @Override
    public boolean blocksInteractionBelow(@NonNull CoordinatorLayout parent, @NonNull View child) {
        Log.e("tag","blocksInteractionBelow");
        return getScrimOpacity(parent, child) > 0.f;
    }

    @Override
    public void onDependentViewRemoved(@NonNull CoordinatorLayout parent, @NonNull View child,
                                       @NonNull View dependency) {

        Log.e("tag","onDependentViewRemoved");
    }

    @Override
    public boolean onMeasureChild(@NonNull CoordinatorLayout parent, @NonNull View child,
                                  int parentWidthMeasureSpec, int widthUsed,
                                  int parentHeightMeasureSpec, int heightUsed) {

        Log.e("tag","onMeasureChild");

        return false;
    }

    @Override
    public boolean onLayoutChild(@NonNull CoordinatorLayout parent, @NonNull View child,
                                 int layoutDirection) {

        Log.e("tag","onLayoutChild");


        return false;
    }

    // Utility methods for accessing child-specific, behavior-modifiable properties.


    @Override
    @Deprecated
    public boolean onStartNestedScroll(@NonNull CoordinatorLayout coordinatorLayout,
                                       @NonNull View child, @NonNull View directTargetChild, @NonNull View target,
                                       @ViewCompat.ScrollAxis int axes) {

        Log.e("tag","onStartNestedScroll1");
        return false;
    }


    @Override
    public boolean onStartNestedScroll(@NonNull CoordinatorLayout coordinatorLayout,
                                       @NonNull View child, @NonNull View directTargetChild, @NonNull View target,
                                       @ViewCompat.ScrollAxis int axes, @ViewCompat.NestedScrollType int type) {
        if (type == ViewCompat.TYPE_TOUCH) {
            return onStartNestedScroll(coordinatorLayout, child, directTargetChild,
                    target, axes);
        }
        Log.e("tag","onStartNestedScroll2");
        return false;
    }

    @Override
    @Deprecated
    public void onNestedScrollAccepted(@NonNull CoordinatorLayout coordinatorLayout,
                                       @NonNull View child, @NonNull View directTargetChild, @NonNull View target,
                                       @ViewCompat.ScrollAxis int axes) {

        Log.e("tag","onNestedScrollAccepted1");
        // Do nothing
    }

    @Override
    public void onNestedScrollAccepted(@NonNull CoordinatorLayout coordinatorLayout,
                                       @NonNull View child, @NonNull View directTargetChild, @NonNull View target,
                                       @ViewCompat.ScrollAxis int axes, @ViewCompat.NestedScrollType int type) {
        if (type == ViewCompat.TYPE_TOUCH) {
            onNestedScrollAccepted(coordinatorLayout, child, directTargetChild,
                    target, axes);
        }

        Log.e("tag","onNestedScrollAccepted2");
    }

    @Override
    @Deprecated
    public void onStopNestedScroll(@NonNull CoordinatorLayout coordinatorLayout,
                                   @NonNull View child, @NonNull View target) {
        // Do nothing

        Log.e("tag","onStopNestedScroll1");
    }

    @Override
    public void onStopNestedScroll(@NonNull CoordinatorLayout coordinatorLayout,
                                   @NonNull View child, @NonNull View target, @ViewCompat.NestedScrollType int type) {
        if (type == ViewCompat.TYPE_TOUCH) {
            onStopNestedScroll(coordinatorLayout, child, target);
        }

        Log.e("tag","onStopNestedScroll2");
    }

    @Override
    @Deprecated
    public void onNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child,
                               @NonNull View target, int dxConsumed, int dyConsumed, int dxUnconsumed,
                               int dyUnconsumed) {
        // Do nothing

        Log.e("tag","onNestedScroll1");
    }

    @Override
    @Deprecated
    public void onNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child,
                               @NonNull View target, int dxConsumed, int dyConsumed, int dxUnconsumed,
                               int dyUnconsumed, @ViewCompat.NestedScrollType int type) {
        if (type == ViewCompat.TYPE_TOUCH) {
            onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed,
                    dxUnconsumed, dyUnconsumed);
        }

        Log.e("tag","onNestedScroll2");
    }

    @Override
    public void onNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child,
                               @NonNull View target, int dxConsumed, int dyConsumed, int dxUnconsumed,
                               int dyUnconsumed, @ViewCompat.NestedScrollType int type, @NonNull int[] consumed) {
        // In the case that this nested scrolling v3 version is not implemented, we call the v2
        // version in case the v2 version is. We Also consume all of the unconsumed scroll
        // distances.
        consumed[0] += dxUnconsumed;
        consumed[1] += dyUnconsumed;
        onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed,
                dxUnconsumed, dyUnconsumed, type);

        Log.e("tag","onNestedScroll3");
    }

    @Override
    @Deprecated
    public void onNestedPreScroll(@NonNull CoordinatorLayout coordinatorLayout,
                                  @NonNull View child, @NonNull View target, int dx, int dy, @NonNull int[] consumed) {
        // Do nothing

        Log.e("tag","onNestedPreScroll1");
    }


    @Override
    public void onNestedPreScroll(@NonNull CoordinatorLayout coordinatorLayout,
                                  @NonNull View child, @NonNull View target, int dx, int dy, @NonNull int[] consumed,
                                  @ViewCompat.NestedScrollType int type) {
        if (type == ViewCompat.TYPE_TOUCH) {
            onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed);
        }

        Log.e("tag","onNestedPreScroll2");
    }


    @Override
    public boolean onNestedFling(@NonNull CoordinatorLayout coordinatorLayout,
                                 @NonNull View child, @NonNull View target, float velocityX, float velocityY,
                                 boolean consumed) {

        Log.e("tag","onNestedFling");
        return false;
    }

    @Override
    public boolean onNestedPreFling(@NonNull CoordinatorLayout coordinatorLayout,
                                    @NonNull View child, @NonNull View target, float velocityX, float velocityY) {

        Log.e("tag","onNestedPreFling");
        return false;
    }

    @Override
    @NonNull
    public WindowInsetsCompat onApplyWindowInsets(@NonNull CoordinatorLayout coordinatorLayout,
                                                  @NonNull View child, @NonNull WindowInsetsCompat insets) {
        Log.e("tag","onApplyWindowInsets");
        return insets;
    }

    @Override
    public boolean onRequestChildRectangleOnScreen(@NonNull CoordinatorLayout coordinatorLayout,
                                                   @NonNull View child, @NonNull Rect rectangle, boolean immediate) {
        Log.e("tag","onRequestChildRectangleOnScreen");

        return false;
    }

    @Override
    public void onRestoreInstanceState(@NonNull CoordinatorLayout parent, @NonNull View child,
                                       @NonNull Parcelable state) {

        Log.e("tag","onRestoreInstanceState");

        // no-op
    }

    /**
     * Hook allowing a behavior to generate a representation of its internal state
     * that can later be used to create a new instance with that same state.
     * This state should only contain information that is not persistent or can
     * not be reconstructed later.
     *
     * <p>Behavior state is only saved when both the parent {@link CoordinatorLayout} and
     * a view using this behavior have valid IDs set.</p>
     *
     * @param parent the parent CoordinatorLayout
     * @param child  child view to restore from
     * @return Returns a Parcelable object containing the behavior's current dynamic
     * state.
     */
    @Nullable
    public Parcelable onSaveInstanceState(@NonNull CoordinatorLayout parent, @NonNull V child) {

        Log.e("tag","onSaveInstanceState");
        return View.BaseSavedState.EMPTY_STATE;
    }

    /**
     * Called when a view is set to dodge view insets.
     *
     * <p>This method allows a behavior to update the rectangle that should be dodged.
     * The rectangle should be in the parent's coordinate system and within the child's
     * bounds. If not, a {@link IllegalArgumentException} is thrown.</p>
     *
     * @param parent the CoordinatorLayout parent of the view this Behavior is
     *               associated with
     * @param child  the child view of the CoordinatorLayout this Behavior is associated with
     * @param rect   the rect to update with the dodge rectangle
     * @return true the rect was updated, false if we should use the child's bounds
     */
    public boolean getInsetDodgeRect(@NonNull CoordinatorLayout parent, @NonNull V child,
                                     @NonNull Rect rect) {

        Log.e("tag","getInsetDodgeRect");
        return false;
    }

}
